<template>
  <div>
    <div style="margin-top: 12px">
      <el-input style="width: 200px" placeholder="请输入名称" suffix-icon="el-icon-search" v-model="username"></el-input>
      <el-input style="width: 200px" placeholder="请输入昵称" suffix-icon="el-icon-user"  class="ml-5" v-model="nickname"></el-input>
      <el-input style="width: 200px" placeholder="请输入昵称" suffix-icon="el-icon-message"  class="ml-5" v-model="email"></el-input>
      <el-input style="width: 200px" placeholder="请输入地址" suffix-icon="el-icon-position"  class="ml-5" v-model="address"></el-input>
      <el-button style="margin-left: 5px" type="primary" @click="load">搜索</el-button>
      <el-button style="margin-left: 5px" type="warning" @click="reset">重置</el-button>
    </div>
    <div style="margin: 6px 0">
      <el-button type="primary" @click="handleAdd">新增<i class="el-icon-circle-plus-outline"></i></el-button>
      <el-popconfirm title="确认批量删除吗?" style="margin: 0 10px" @confirm="delBatch">
        <template #reference>
          <el-button type="danger" >批量删除<i class="el-icon-remove-outline"></i></el-button>
        </template>
      </el-popconfirm>
      <el-upload action="http://localhost:9090/admin/import" :show-file-list="false"
                 accept=".xlsx" :on-success="handleExcelImportSuccess"
                 style="display: inline-block">
        <el-button type="primary">导入<i class="el-icon-bottom"></i></el-button>
      </el-upload>
      <el-button type="primary" @click="exp" style="margin-left: 10px">导出<i class="el-icon-top"></i></el-button>
    </div>
    <el-table :data="tableData" border stripe @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55"></el-table-column>
      <el-table-column prop="id" label="id" width="80">
      </el-table-column>
      <el-table-column prop="username" label="姓名" width="100">
      </el-table-column>
      <el-table-column prop="nickname" label="昵称" width="140">
      </el-table-column>
      <el-table-column prop="email" label="邮箱" width="140">
      </el-table-column>
      <el-table-column prop="phone" label="电话" width="140">
      </el-table-column>
      <el-table-column prop="address" label="地址">
      </el-table-column>
      <el-table-column
          fixed="right"
          label="操作"
          width="170">
        <template #default = "scope">
          <el-button size="small" type="text" @click="selectMenu(scope.row.id)"
                     style="padding: 2px; color: #8c939d"><i class="el-icon-menu" ></i>分配菜单</el-button>
          <el-button size="small" type="text" @click="handleEdit(scope.row)"  style="padding: 2px">编辑</el-button>
          <el-popconfirm title="确认删除吗?" @confirm="handleDelete(scope.row.id)">
            <template #reference>
              <el-button size="small"  type="text" style="padding: 2px">删除</el-button>
            </template>
          </el-popconfirm>
        </template>
      </el-table-column>
    </el-table>
    <div style="padding: 2px 0">
      <el-pagination
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"
          :current-page="pageNum"
          :page-sizes="[2, 4, 6, 8]"
          :page-size="pageSize"
          layout="total, sizes, prev, pager, next, jumper"
          :total="total">
      </el-pagination>
    </div>
    <el-dialog title="用户信息" :visible.sync="dialogFormVisible" width="30%">
      <el-form :model="form" label-width="60px" >
        <el-form-item label="用户名">
          <el-input v-model="form.username" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="昵称">
          <el-input v-model="form.nickname" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="性别">
          <el-radio-group v-model="form.sex">
            <el-radio label="男"></el-radio>
            <el-radio label="女"></el-radio>
            <el-radio label="外星人"></el-radio>
          </el-radio-group>
        </el-form-item>
        <el-form-item label="邮箱">
          <el-input v-model="form.email" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="电话">
          <el-input v-model="form.phone" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="地址">
          <el-input v-model="form.address" autocomplete="off"></el-input>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogFormVisible = false; form = []">取 消</el-button>
        <el-button type="primary" @click="save">确 定</el-button>
      </div>
    </el-dialog>
    <el-dialog title="菜单分配" :visible.sync="menuFormVisible" width="30%">
      <el-tree
          :data="menuData"
          :props="props"
          show-checkbox
          node-key="id"
          ref="tree"
          :default-expanded-keys="expends"
          :default-checked-keys="checks">
        <span class="custom-tree-node" slot-scope="{node, data}">
          <span><i :class="data.icon"></i>{{ data.name }} </span>
        </span>
      </el-tree>

      <div slot="footer" class="dialog-footer">
        <el-button @click="menuFormVisible = false; form = []">取 消</el-button>
        <el-button type="primary" @click="saveAdminMenu">确 定</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<style>
.el-dialog__body{
  padding-bottom: 5px;
}
</style>

<script>
export default {
  name: "Admin",
  data() {
    return {
      tableData: [],
      total:0,
      username:"",
      nickname:"",
      email:"",
      address:"",
      pageSize:4,
      pageNum:1,
      form:{},
      dialogFormVisible: false,
      menuFormVisible: false,
      multipleSelection: [],
      menuData:[],
      props: {
        label: 'name',
      },
      expends: [],
      checks: [],
      adminId: 0,
    }
  },
  created() {
    this.load();
  },
  methods :{
    // 请求分页查询数据
    load(){
      this.request.get("admin/page",{
        params: {
          pageNum: this.pageNum,
          pageSize: this.pageSize,
          username: this.username,
          nickname: this.nickname,
          address: this.address,
          email: this.email
        }
      }).then(res => {
        this.tableData = res.data.records
        this.total = res.data.total
      })

    },
    // 保存
    save() {
      this.request.post("/admin", this.form).then(res =>{
        if(res.data) {
          this.$message.success("保存成功")
        } else {
          this.$message.error("保存失败")
        }
        this.load()
      })
      this.dialogFormVisible = false;
      this.form = {};
    },
    //编辑
    handleEdit(row){
      this.form = JSON.parse(JSON.stringify(row)) //深拷贝
      this.form.id = row.id;
      this.dialogFormVisible = true;
    },
    // 删除
    handleDelete(id){
      this.request.delete("/admin/" + id).then(res => {
        if (res.data) {
          this.$message.success("删除成功"
          )
        } else {
          this.$message.error("删除失败")
        }
        this.load()
      })
    },
    //实现批量选择
    handleSelectionChange(val) {
      console.log(val)
      this.multipleSelection = val
    },
    //批量删除
    delBatch() {
      let ids = this.multipleSelection.map(v => v.id) // 得到每个选择对象的 id
      console.log(ids)
      this.request.post("/admin/del/batch", ids).then(res => {
        if (res.data) {
          this.$message.success("批量删除成功")
        } else {
          this.$message.error("批量删除失败")
        }
        this.load()
      })
    },
    // 导出
    exp() {
      window.open("http://localhost:9090/admin/export")
    },
    // 导入
    handleExcelImportSuccess() {
      this.$message.success("文件导入成功")
      this.load()
    },
    // 改变页大小
    handleSizeChange(pageSize) {
      this.pageSize = pageSize
      this.load();
    },
    // 改变当前页
    handleCurrentChange(pageNum) {
      this.pageNum = pageNum
      this.load();
    },
    // 重新设置搜索框
    reset() {
      this.username = ""
      this.nickname = ""
      this.email = ""
      this.address = ""
      this.load()
    },
    // 打开新增表单
    handleAdd() {
      this.dialogFormVisible = true
    },
    selectMenu(adminId) {
      this.menuFormVisible = true;
      this.adminId = adminId
      this.request.get("/menu").then(res => {
        this.menuData = res.data
        //把后台返回的菜单数据处理成 id 数组
        this.expends = this.menuData.map(v => v.id)
      })
      this.request.get("/admin/adminMenu/" + adminId).then(res => {
        this.checks = res.data
      })
    },

    //
    saveAdminMenu () {
      this.request.post("admin/adminMenu/" + this.adminId, this.$refs.tree.getCheckedKeys()).then(res => {
        if (res.code === '200') {
          this.$message.success("绑定成功")
          this.menuFormVisible = false
        } else {
          this.$message.error(res.data)
        }
      })
    }
  }
}
</script>